/**
 * Project Name:jbase
 * File Name:HttpClient.java
 * Package Name:jbase.project.system.net.http.websearch
 * Date:2017年4月30日下午6:31:44
 * Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
 *
*/

package jbase.project.system.net.http.websearcher;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.security.Permission;
import java.util.List;
import java.util.Map;

import jbase.project.system.exception.SysException;

/**
 * ClassName:HttpClient <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2017年4月30日 下午6:31:44 <br/>
 * 
 * @author Licheng
 * @version
 * @since JDK 1.8
 * @see
 */
public class HttpClient {

	private HttpURLConnection httpURLConnection = null;
	private String url = null;

	public void setRequestHeaders(Map<String, String> headers) {
		for (String headerKey : headers.keySet()) {
			httpURLConnection.setRequestProperty(headerKey, headers.get(headerKey));
		}
	}

	public HttpClient(String url) throws SysException {
		this.url = url;
		try {
			this.httpURLConnection = (HttpURLConnection) new URL(this.url).openConnection();
		} catch (MalformedURLException e) {
			throw new SysException(e);
		} catch (IOException e) {
			throw new SysException(e);
		}
	}

	public HttpClient(String url, Proxy proxy) throws MalformedURLException, IOException {
		this.url = url;
		this.httpURLConnection = (HttpURLConnection) new URL(this.url).openConnection(proxy);
	}

	public HttpResponse doGetResponse() throws SysException {
		try {
			httpURLConnection.setRequestMethod("GET");
			setDoInput(true);
			setDoOutput(true);
			httpURLConnection.connect();
			InputStream is = httpURLConnection.getInputStream();
			Map<String, List<String>> headers = httpURLConnection.getHeaderFields();

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int readCount;
			while ((readCount = is.read(buff)) != -1) {
				baos.write(buff, 0, readCount);
			}
			is.close();
			httpURLConnection.disconnect();
			HttpResponse response = new HttpResponse(headers, baos.toByteArray());
			return response;
		} catch (ProtocolException e) {
			throw new SysException(e);
		} catch (IOException e) {
			throw new SysException(e);
		}
	}

	public int hashCode() {
		return httpURLConnection.hashCode();
	}

	public String getHeaderFieldKey(int n) {
		return httpURLConnection.getHeaderFieldKey(n);
	}

	public boolean equals(Object obj) {
		return httpURLConnection.equals(obj);
	}

	public void setFixedLengthStreamingMode(int contentLength) {
		httpURLConnection.setFixedLengthStreamingMode(contentLength);
	}

	public void setFixedLengthStreamingMode(long contentLength) {
		httpURLConnection.setFixedLengthStreamingMode(contentLength);
	}

	public void setChunkedStreamingMode(int chunklen) {
		httpURLConnection.setChunkedStreamingMode(chunklen);
	}

	public String getHeaderField(int n) {
		return httpURLConnection.getHeaderField(n);
	}

	public void setInstanceFollowRedirects(boolean followRedirects) {
		httpURLConnection.setInstanceFollowRedirects(followRedirects);
	}

	public void setConnectTimeout(int timeout) {
		httpURLConnection.setConnectTimeout(timeout);
	}

	public boolean getInstanceFollowRedirects() {
		return httpURLConnection.getInstanceFollowRedirects();
	}

	public void setRequestMethod(String method) throws ProtocolException {
		httpURLConnection.setRequestMethod(method);
	}

	public int getConnectTimeout() {
		return httpURLConnection.getConnectTimeout();
	}

	public void setReadTimeout(int timeout) {
		httpURLConnection.setReadTimeout(timeout);
	}

	public int getReadTimeout() {
		return httpURLConnection.getReadTimeout();
	}

	public String getRequestMethod() {
		return httpURLConnection.getRequestMethod();
	}

	public int getResponseCode() throws IOException {
		return httpURLConnection.getResponseCode();
	}

	public URL getURL() {
		return httpURLConnection.getURL();
	}

	public int getContentLength() {
		return httpURLConnection.getContentLength();
	}

	public long getContentLengthLong() {
		return httpURLConnection.getContentLengthLong();
	}

	public String getContentType() {
		return httpURLConnection.getContentType();
	}

	public String getContentEncoding() {
		return httpURLConnection.getContentEncoding();
	}

	public long getExpiration() {
		return httpURLConnection.getExpiration();
	}

	public String getResponseMessage() throws SysException {
		try {
			return httpURLConnection.getResponseMessage();
		} catch (IOException e) {
			throw new SysException(e);
		}
	}

	public long getDate() {
		return httpURLConnection.getDate();
	}

	public long getHeaderFieldDate(String name, long Default) {
		return httpURLConnection.getHeaderFieldDate(name, Default);
	}

	public long getLastModified() {
		return httpURLConnection.getLastModified();
	}

	public void disconnect() {
		httpURLConnection.disconnect();
	}

	public String getHeaderField(String name) {
		return httpURLConnection.getHeaderField(name);
	}

	public boolean usingProxy() {
		return httpURLConnection.usingProxy();
	}

	public Permission getPermission() throws IOException {
		return httpURLConnection.getPermission();
	}

	public Map<String, List<String>> getHeaderFields() {
		return httpURLConnection.getHeaderFields();
	}

	public int getHeaderFieldInt(String name, int Default) {
		return httpURLConnection.getHeaderFieldInt(name, Default);
	}

	public InputStream getErrorStream() {
		return httpURLConnection.getErrorStream();
	}

	public long getHeaderFieldLong(String name, long Default) {
		return httpURLConnection.getHeaderFieldLong(name, Default);
	}

	public Object getContent() throws SysException {
		try {
			return httpURLConnection.getContent();
		} catch (IOException e) {
			throw new SysException(e);
		}
	}

	public Object getContent(Class<?>[] classes) throws IOException {
		return httpURLConnection.getContent(classes);
	}

	public InputStream getInputStream() throws IOException {
		return httpURLConnection.getInputStream();
	}

	public OutputStream getOutputStream() throws IOException {
		return httpURLConnection.getOutputStream();
	}

	public String toString() {
		return httpURLConnection.toString();
	}

	public boolean getDoInput() {
		return httpURLConnection.getDoInput();
	}

	public boolean getDoOutput() {
		return httpURLConnection.getDoOutput();
	}

	public void setAllowUserInteraction(boolean allowuserinteraction) {
		httpURLConnection.setAllowUserInteraction(allowuserinteraction);
	}

	public boolean getAllowUserInteraction() {
		return httpURLConnection.getAllowUserInteraction();
	}

	public void setUseCaches(boolean usecaches) {
		httpURLConnection.setUseCaches(usecaches);
	}

	public boolean getUseCaches() {
		return httpURLConnection.getUseCaches();
	}

	public void setIfModifiedSince(long ifmodifiedsince) {
		httpURLConnection.setIfModifiedSince(ifmodifiedsince);
	}

	public long getIfModifiedSince() {
		return httpURLConnection.getIfModifiedSince();
	}

	public boolean getDefaultUseCaches() {
		return httpURLConnection.getDefaultUseCaches();
	}

	public void setDefaultUseCaches(boolean defaultusecaches) {
		httpURLConnection.setDefaultUseCaches(defaultusecaches);
	}

	public void setRequestProperty(String key, String value) {
		httpURLConnection.setRequestProperty(key, value);
	}

	public void addRequestProperty(String key, String value) {
		httpURLConnection.addRequestProperty(key, value);
	}

	public String getRequestProperty(String key) {
		return httpURLConnection.getRequestProperty(key);
	}

	public Map<String, List<String>> getRequestProperties() {
		return httpURLConnection.getRequestProperties();
	}

	public void setDoInput(boolean enabled) {
		httpURLConnection.setDoInput(enabled);
	}

	public void setDoOutput(boolean enabled) {
		httpURLConnection.setDoOutput(enabled);
	}

	public void connect() throws SysException {
		try {
			httpURLConnection.connect();
		} catch (IOException e) {
			throw new SysException(e);
		}
	}
}